package com.haochang.chunkvod.controller.activity.order;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseGridView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.database.order.PartInfoDao;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.order.PartInfo;
import com.haochang.chunkvod.view.vod.popupWindow.VodPopWindow;
import com.haochang.chunkvod.view.vod.title.TitleView;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

/**
 * @author qintao
 *         created at 2016/6/7 10:17
 *         分区修改
 */

public class PartModifyActivity extends BaseActivity
{

	private TitleView titleView;
	private BaseGridView select_gridView;
	private BaseGridView no_select_gridView;
	private Button btn_sure;
	private QuickAdapter selectAdapter;
	private QuickAdapter noSelectAdapter;

	private ArrayList<PartInfo> mPartSelectList = new ArrayList<PartInfo>();//已选分区列表
	private ArrayList<PartInfo> mPartNoSelectList = new ArrayList<PartInfo>();//未选分区列表
	private ArrayList<PartInfo> allList = new ArrayList<PartInfo>();//所以分区列表
	private RoomTypeEnum typeEnum = RoomTypeEnum.ROOM;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

	}

	@Override
	public void receiveParam()
	{
		typeEnum = (RoomTypeEnum) getIntent().getSerializableExtra(IntentKey.ROOM_TABLE_TYPE);
	}

	@Override
	public void initData()
	{
		if (typeEnum == RoomTypeEnum.ROOM)
		{
			mPartSelectList = (ArrayList<PartInfo>) PartInfoDao.getDao().queryBuilderAll(PartInfo.class, "is_room_select", true);
			mPartNoSelectList = (ArrayList<PartInfo>) PartInfoDao.getDao().queryBuilderAll(PartInfo.class, "is_room_select", false);
		} else
		{
			mPartSelectList = (ArrayList<PartInfo>) PartInfoDao.getDao().queryBuilderAll(PartInfo.class, "is_table_select", true);
			mPartNoSelectList = (ArrayList<PartInfo>) PartInfoDao.getDao().queryBuilderAll(PartInfo.class, "is_table_select", false);
		}
	}

	@Override
	public void initViews()
	{
		setContentView(R.layout.activity_part_modify);
		titleView = (TitleView) findViewById(R.id.titleView);
		titleView.setTitle(R.string.part_modify).setTitleOnClickListener(new TitleView.ITitleOnClickListener()
		{
			@Override
			public void onBackClick()
			{
				finish();
			}

			@Override
			public void onDoneClick()
			{
				List<String> list = new ArrayList<String>();
				list.add("退出登录");
				int location[] = new int[2];
				ImageView img = (ImageView) titleView.findViewById(R.id.img_operates);
				img.getLocationOnScreen(location);
				int x = location[0];
				final VodPopWindow pop = new VodPopWindow(PartModifyActivity.this, list);
				pop.showPopupWindow(titleView, x);
				pop.setPopWindowListListener(new VodPopWindow.OnPopWindowListListener()
				{
					@Override
					public void onSelectItem(int position)
					{
						switch (position)
						{
						case 0:
							loginOut();
							break;
						default:
							break;
						}
					}
				});
			}
		});
		select_gridView = (BaseGridView) findViewById(R.id.select_gridview);
		no_select_gridView = (BaseGridView) findViewById(R.id.no_select_gridview);
		btn_sure = (Button) findViewById(R.id.btn_sure);
		btn_sure.setOnClickListener(mOnBaseClickListener);
		selectAdapter = new QuickAdapter<PartInfo>(PartModifyActivity.this, R.layout.item_part_modify_grid_layout, mPartSelectList)
		{

			@Override
			protected void convert(BaseAdapterHelper helper, final PartInfo item)
			{
				helper.setText(R.id.tv_partName, item.getPartName());
				helper.getView(R.id.img_del).setVisibility(View.VISIBLE);
				helper.getView(R.id.tv_partName).setOnClickListener(new OnBaseClickListener()
				{
					@Override
					public void onBaseClick(View v)
					{
						if (typeEnum == RoomTypeEnum.ROOM)
							item.setRoomSelect(false);
						else
							item.setTableSelect(false);
						mPartSelectList.remove(item);
						mPartNoSelectList.add(item);
						Collections.sort(mPartNoSelectList, new PartCompare());
						noSelectAdapter.replaceAll(mPartNoSelectList);
						selectAdapter.replaceAll(mPartSelectList);
					}
				});
			}
		};
		select_gridView.setAdapter(selectAdapter);
		noSelectAdapter = new QuickAdapter<PartInfo>(PartModifyActivity.this, R.layout.item_part_modify_grid_layout, mPartNoSelectList)
		{

			@Override
			protected void convert(BaseAdapterHelper helper, final PartInfo item)
			{
				helper.setText(R.id.tv_partName, item.getPartName());
				helper.getView(R.id.img_del).setVisibility(View.GONE);
				helper.getView(R.id.tv_partName).setOnClickListener(new OnBaseClickListener()
				{
					@Override
					public void onBaseClick(View v)
					{
						if (typeEnum == RoomTypeEnum.ROOM)
							item.setRoomSelect(true);
						else
							item.setTableSelect(true);
						mPartSelectList.add(item);
						mPartNoSelectList.remove(item);
						Collections.sort(mPartSelectList, new PartCompare());
						noSelectAdapter.replaceAll(mPartNoSelectList);
						selectAdapter.replaceAll(mPartSelectList);
					}
				});
			}
		};
		no_select_gridView.setAdapter(noSelectAdapter);
	}

	private OnBaseClickListener mOnBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.btn_sure:
				allList.clear();
				allList.addAll(mPartSelectList);
				allList.addAll(mPartNoSelectList);
				try
				{
					PartInfoDao.getDao().updateAll(PartInfo.class, allList);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
				Intent intent = new Intent();
				intent.putParcelableArrayListExtra(IntentKey.PART_DATA, mPartSelectList);
				setResult(RESULT_OK, intent);
				finish();
				break;
			default:
				break;
			}
		}
	};

	public enum RoomTypeEnum
	{
		/**
		 * 房间
		 */
		ROOM,
		/**
		 * 桌台
		 */
		TABLE;
	}

	/**
	* 分区列表排序器
	*/
	class PartCompare implements Comparator<PartInfo>
	{

		@Override
		public int compare(PartInfo p1, PartInfo p2)
		{
			int result = 0;
			if (p1 != null && p2 != null)
			{
				int value = 0;
				if (!TextUtils.isEmpty(String.valueOf(p1.getPart_id())) && !TextUtils.isEmpty(String.valueOf(p2.getPart_id())))
				{
					value = String.valueOf(p2.getPart_id()).compareTo(String.valueOf(p1.getPart_id()));
					if (value != 0)
						result = (value < 0) ? 1 : -1;
				}
			}
			return result;
		}
	}
}
